$(document).ready(function () {
    $('body').height($('body')[0].clientHeight);
});
//一 、检查登录 获取缓存
checkLogin();
var work_info = getCache(login_key);
var title = '';
var title1 = '';
var title2 = '';
var num = '';
var num1 = '';
var num2 = '';
var showcontent = '';
var showcontent1 = '';
var showcontent2 = '';
/*
 二、通过接口获取数据
 1.通过点击导航获取type值,
 2.获取从上个页面传过来的mid
 */
$.init();
var type = 0;
var mid = GetQueryString("mid");
var data = {type:type,mid: mid, wid: work_info.wid, sid: work_info.sid, aid: work_info.aid,from:'wx'};
var dataArr = [];
var  bucket_member = [];
var bill_member = [];
var is_fisrt = GetQueryString('is_fisrt');
if(is_fisrt != undefined && is_fisrt != null && is_fisrt != '' && is_fisrt == 1){
    $('.is_first').css('display','none');
}

$("#bucket_category_id_value").change(function () {
    if (dataArr.length > 0) {
        for (var i = 0; i < dataArr.length; i++) {
            if (dataArr[i]['title'] == $("#bucket_category_id_value").val()) {
                title1 = dataArr[i]['title'];
                $("#bucket_category_id").val(dataArr[i]['bucket_category_id']);
            }
        }
    }
});

$("#self_bucket_category_id_value").change(function () {
    if (dataArr.length > 0) {
        for (var i = 0; i < dataArr.length; i++) {
            if (dataArr[i]['title'] == $("#self_bucket_category_id_value").val()) {
                title2 = dataArr[i]['title'];
                $("#self_bucket_category_id").val(dataArr[i]['bucket_category_id']);
            }
        }
    }
});
$("#waterbill_id_value").change(function () {
    if (dataArr.length > 0) {
        for (var i = 0; i < dataArr.length; i++) {
            if (dataArr[i]['goods_name'] == $("#waterbill_id_value").val()) {
                title = dataArr[i]['goods_name'];
                $("#waterbill_id").val(dataArr[i]['id']);
            }
        }
    }
});
var success = function (res) {
    var title = '请选择水票';
    if (!type) {
        title = '请选择水桶品牌';
        dataArr = res.data.bucket_list;;
    } else {
        dataArr = res.data.bill_list;;
    }
    var values = [];
    for (var i = 0; i < dataArr.length; i++) {
        if (!type) {
            bucket_member = res.data;
            var html = template('bill_data',bucket_member);
            document.getElementById('bucket-block').innerHTML = html;
            if (dataArr[i + 1] != undefined) {
                values[i] = dataArr[i + 1].title;
            }
        } else {
            bill_member = res.data;
            var html = template('bill_data',bill_member);
            document.getElementById('bill-block').innerHTML = html;
            values[i] = dataArr[i].goods_name;
        }
    }
    var className = ".picker_" + type;
    $(className).picker({
        toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left  close-picker">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          <h1 class="title">' + title + '</h1>\
          </header>',
        cols: [
            {
                textAlign: 'center',
                values: values
            }
        ]
    });

    if (!type) {
        $(className+'_1').picker({
            toolbarTemplate: '<header class="bar bar-nav">\
          <button class="button button-link pull-left  close-picker">取消</button>\
          <button class="button button-link pull-right close-picker">确定</button>\
          <h1 class="title">' + title + '</h1>\
          </header>',
            cols: [
                {
                    textAlign: 'center',
                    values: values,
                }
            ]
        });
    }
};
getData(worker_select_data, data, success);


//验证表单并处理登录交互

function numValue(){
    if(type == 1){
        num = $('#nums').val();
        if(title && num){
            showcontent = '<p>'+'水票名称：'+ title +'</p>'+'<p>'+'水票数量： '+ num +' '+'张'+'</p>' ;
        }
        return '<p>'+showcontent+'</p>';
    }
    if(type == 0){
        num1 = $('#nums1').val();
        num2 = $('#self_bucket_num').val();
        if(title1 && num1){
            showcontent1 = title1 +'  '+ num1 +' '+'个';
        }
        if(title2 && num2){
            showcontent2 = title2 +'  '+ num2 +' '+'个';
        }
        return '<p>'+showcontent1+'</p>'+'<p>'+showcontent2+'</p>';
    }

}
var ajaxOption = {
    url: worker_import,
    method: 'post',
    confirm:true,
    confirm_title:'数据确认',
    confirm_content:['numValue'],//用[]包起来表示该值是函数，不包则为字符串
    data:data,
    success: function (res) {
        if(type == 0){ //表示为初始水桶的导入
            if(is_fisrt != undefined && is_fisrt != null && is_fisrt != '' && is_fisrt == 1){
                var o_id = GetQueryString('o_id');
                if(o_id != undefined && o_id != null && o_id != ''){
                    var href = 'order-detail.html?o_id='+o_id;
                    redirect(href);
                }
            }
        }
    },
};

checkform($('#form1'), ajaxOption);

checkform($('#form0'), ajaxOption);


$('.buttons-tab').find('a').click(function () {
    var oldType = type;
    type = parseInt($(this).attr('data-type'));
    if (oldType != type) {
        eval("data.type="+type);
        getData(worker_select_data, data, success);
    }

});
    
